package cn.hiapi.shop.api;

import cn.hiapi.basic.ResponseEntity;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.entity.BasicEntity;
import cn.hiapi.basic.exception.BasicException;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.basic.token.Token;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.interfaces.user.IUserService;
import cn.hiapi.shop.query.ActivityOrderQuery;
import cn.hiapi.shop.service.shop.ShopLinkUserService;
import cn.hiapi.shopkeeper.entity.order.ActivityGroupOrder;
import cn.hiapi.shopkeeper.entity.order.ActivityOrder;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.service.activity.ActivityCodeRecordService;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.order.ActivityGroupOrderService;
import cn.hiapi.shopkeeper.service.order.ActivityOrderService;
import cn.hiapi.user.entity.User;
import cn.hiapi.user.vo.ActivityOrderGroupVo;
import cn.hiapi.user.vo.ActivityOrderVo;
import cn.hiapi.utils.JsonUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2023/2/5 21:02
 */
@RestController
@RequestMapping("/shop/order")
public class ShopActivityOrderController extends BasicQueryController<ActivityOrder, Long, ActivityOrderVo, ActivityOrderQuery> {
    private final ActivityOrderService basicService;


    protected ShopActivityOrderController(ActivityOrderService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
        this.basicService = basicService;
    }


    @Override
    public ActivityOrderQuery setQuery(ActivityOrderQuery query) {
        Token<User> token = TokenGet.get();
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(token.getFid(), query.getShopId());

        if (StringUtils.isNotEmpty(query.getKey())) {
            if (StringUtils.isNumeric(query.getKey())) {
                if (StringUtils.length(query.getKey()) == 11) {
                    Long uid = this.dispatchContext.getServiceOne(IUserService.class).getUid(token.getMerchantId(), query.getKey());
                    if (uid == null) throw new BasicException("手机号不存在");
                    query.setBuyerId(uid);
                } else query.setOid(Long.valueOf(query.getKey()));
            } else {
                query.setTitle(query.getKey());
            }
        }
        return super.setQuery(query);
    }

    @Override
    public List<ActivityOrderVo> toListVo(List<ActivityOrder> list, String[] fields) {
        return this.toList(list,fields);
    }

    @Override
    public ActivityOrderVo toData(ActivityOrder data, String[] fields) {
        ActivityOrderVo vo = data.copy(new ActivityOrderVo());
        if (data.getActivityType() == ActivityType.GROUP) {
            ActivityOrderGroupVo group = new ActivityOrderGroupVo();
            if (data.getFid() != null) {
                group.setItems(basicService.groupUserList(data.getFid(),data.getStatus()));
                ActivityGroupOrder order = this.dispatchContext.getServiceOne(ActivityGroupOrderService.class).get(data.getFid());
                if (order != null) {
                    group.setGroupNum(order.getGroupNum());
                    group.setExpire(order.getExpire());
                    group.setId(order.getId());
                }
                vo.setGroup(group);
            }
        }else if (data.getActivityType() == ActivityType.CARD) {
            JSONObject object;
            if (!StringUtils.isEmpty(data.getExpandText())) {
                object = JSON.parseObject(data.getExpandText());
            } else {
                BasicEntity entity = this.dispatchContext.getServiceOne(ActivityFactory.class).get(data.getActivityType()).getByAid(data.getAid());
                object = JSON.parseObject(entity.toJSONString());
            }
            List<JSONObject> card = this.dispatchContext.getServiceOne(ActivityCodeRecordService.class).totalCount(data.getId());
            JSONObject cardObj = JsonUtils.toObject(card, e -> JsonUtils.builder().put("status_" + e.getIntValue("status"), e.getIntValue("count")).build());
            object.put("codeTotal", cardObj);
            vo.setExpand(object);
        }
        vo.setUser(this.dispatchContext.getServiceOne(IUserService.class).fieldBase(data.getBuyerId()));
        return vo;
    }


}
